﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OOAD_Project_Editor.Application;
using OOAD_Project_Engine.Domain.Components;
using OOAD_Project_Engine.Foundation.Interfaces;

namespace OOAD_Project_Editor.Presentation
{
    public partial class EntityComponentPropertyForm : Form
    {
        IEntity currentEntity { get; set; }
        EditorForm form;
        public EntityComponentPropertyForm(EditorForm form)
        {
            InitializeComponent();
            this.form = form;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((int)msg.LParam == 0x203e0001)   // Alt-F4
                return true;
            return base.ProcessCmdKey(ref msg, keyData);
        }

        public void SetData(IEntity entity)
        {
            listView1.Items.Clear();
            currentEntity = entity;
            if (currentEntity != null)
            {
                entityComponentPropertyGrid.SelectedObject = null;
                _entityComponentLabel.Text = entity.Name + "-components:";
                Dictionary<Type, IEntityComponent> components = new Dictionary<Type, IEntityComponent>();
                foreach (IEntityComponent c in entity)
                {
                    components.Add(c.GetType(), c);
                }
                SetDictionary(components);
            }
            if (listView1.Items.Count > 0)
                SetSelectedComponent(listView1.Items[0].Tag);
        }

        private void SetDictionary(Dictionary<Type, IEntityComponent> components)
        {
            foreach (Type t in components.Keys)
            {
                IEntityComponent ec = components[t];
                ListViewItem item = new ListViewItem(t.Name, 0);
                item.Tag = ec;
                listView1.Items.Add(item);
                SetSelectedComponent(ec);
            }
        }

        private void SetSelectedComponent(object data)
        {
            entityComponentPropertyGrid.SelectedObject = data;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectItem != null)
                SetSelectedComponent(SelectItem.Tag);
        }

        private ListViewItem SelectItem
        {
            get
            {
                ListViewItem _item = null;
                foreach (ListViewItem item in listView1.SelectedItems) //多選會是最後一個
                    _item = item;
                return _item;
            }
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenuStrip con = new ContextMenuStrip();
                ToolStripItem item = createItem(SelectItem.Tag as IEntityComponent, "RemoveComponent");
                item.Click += removeEntityComponentAction;
                con.Items.Add(item);
                con.Show(new Point(this.Location.X + e.Location.X + 10, this.Location.Y + e.Location.Y + 32));
            }
        }

        private void removeEntityComponentAction(object sender, EventArgs e)
        {
            ToolStripItem item = sender as ToolStripItem;
            IEntityComponent component = item.Tag as IEntityComponent;
            form.RemoveEntityComponent(component);
        }

        private ToolStripItem createItem(IEntityComponent component, string text)
        {
            ToolStripItem item = new ToolStripMenuItem();
            item.Name = text;
            item.Text = text;
            item.Tag = component;
            return item;
        }

    }
}
